using UnityEngine;
using System.Collections;
using System;

public class CharacterGenerator : MonoBehaviour {
	private PlayerCharacter _toon;
	private const int STARTING_POINTS = 350;													//points left to put
	private const int MIN_STARTING_ATRIBUTE_VALUE = 10;							  //min attribut points
	private const int STARTING_VALUE = 50;														//tarting points
	private int pointLeft;	
	private const int OFFSET = 5;
	private const int LINE_HEIGHT = 20;
	private const int STAT_LABLE_WIDTH = 100;
	private const int BASEVALUE_LABLE_WIDTH = 30;
	private const int INSET = 255;
	private const int BUTTON_WIFTH = 20;
	private const int BUTTON_HEIGHT = 20;
	private const int STAT_STARTING_POINT = 40;
	
	
	public GUISkin mySkin;	
	public GameObject playerPreFab;	
	
	// Use this for initialization
	void Start () {
		GameObject pc = Instantiate(playerPreFab, Vector3.zero, Quaternion.identity) as GameObject;
		pc.name = "PC";
		
		//_toon = new PlayerCharacter();
		//_toon.Awake();
		_toon = pc.GetComponent<PlayerCharacter>();
		
		pointLeft = STARTING_POINTS;
			for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
				_toon.GetPrimaryAttribute(cnt).BaseValue = STARTING_VALUE;
				pointLeft -= (STARTING_VALUE - MIN_STARTING_ATRIBUTE_VALUE);
			}		
					_toon.StatUpdate();
	}
	
	// Update is called once per frame
	void Update () {
	
	}	
	 void OnGUI() {
		GUI.skin = mySkin;
		DislayName();
		DispplayPointLeft();
		DisplayAttribute();
		DisplayVitals();
		DisplaySkills();
		DisplayButton();
		}	
		private void DislayName() {
			GUI.Label(new Rect(10, 10, 50, 25), "Name:");
				_toon.Name = GUI.TextField(new Rect(65, 10, 100, 25), _toon.Name);
		}	
	private void DisplayAttribute() {
		for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
                         GUI.Label(new Rect(OFFSET,													 //x
			                   STAT_STARTING_POINT + (cnt * LINE_HEIGHT),					//y
                         STAT_LABLE_WIDTH,										               //WIDTH
			                   LINE_HEIGHT),												               //height
			                   ((AttributeName)cnt).ToString());
			
			GUI.Label(new Rect(STAT_LABLE_WIDTH + OFFSET,								        	//x
			                   STAT_STARTING_POINT + (cnt * LINE_HEIGHT),					//y
			                   BASEVALUE_LABLE_WIDTH,										          //WIDTH
			                   LINE_HEIGHT),												               //height
			          		   _toon.GetPrimaryAttribute(cnt).AdjustedBaseValue.ToString() );
			
			if(GUI.Button(new Rect(OFFSET + STAT_LABLE_WIDTH + BASEVALUE_LABLE_WIDTH,		//x
			                       STAT_STARTING_POINT + (cnt * BUTTON_HEIGHT),				  //y
			                       BUTTON_WIFTH,											                   //WIDTH
			                       BUTTON_HEIGHT), "-")) {									             //height
			if(_toon.GetPrimaryAttribute(cnt).BaseValue > MIN_STARTING_ATRIBUTE_VALUE) {
				_toon.GetPrimaryAttribute(cnt).BaseValue--;
					pointLeft++;
						_toon.StatUpdate();
						}
			}
			if(GUI.Button(new Rect(OFFSET + STAT_LABLE_WIDTH + BASEVALUE_LABLE_WIDTH * 2,	//x
			                       STAT_STARTING_POINT + (cnt * BUTTON_HEIGHT),				    //y
			                       BUTTON_WIFTH,											                     //WIDTH
			                       BUTTON_HEIGHT), "+")) {									               //height
				if(pointLeft > 0) {
					_toon.GetPrimaryAttribute(cnt).BaseValue++;
						pointLeft--;
						   _toon.StatUpdate();
				}
			}
		}
	}
	
	private void DisplayVitals() {
		for(int cnt = 0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++) {
			GUI.Label(new Rect(OFFSET,														                    //x
			                   STAT_STARTING_POINT + ((cnt +7) * LINE_HEIGHT),				//y
			                   STAT_LABLE_WIDTH,											                //WIDTH
			                   LINE_HEIGHT),												                  //height
							   ((VitalName)cnt).ToString());
			
			GUI.Label(new Rect(STAT_LABLE_WIDTH + OFFSET,									            //x
			                   STAT_STARTING_POINT + ((cnt +7) * LINE_HEIGHT)				  //y
			                   , BASEVALUE_LABLE_WIDTH,										            //WIDTH
			                   LINE_HEIGHT),												                   //height
			          		   _toon.GetVital(cnt).AdjustedBaseValue.ToString() );
		
		}
	}
	
	private void DisplaySkills() {
		for(int cnt = 0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++) {
			GUI.Label(new Rect(INSET,														                   //x
			                   STAT_STARTING_POINT + (cnt * LINE_HEIGHT),				 	//y
			                   STAT_LABLE_WIDTH,											              //WIDTH
			                   LINE_HEIGHT),												                //height
			          		   ((SkillName)cnt).ToString());
			GUI.Label(new Rect(STAT_LABLE_WIDTH + INSET,									          //x
			                   STAT_STARTING_POINT + (cnt * LINE_HEIGHT),			 		//y
			                   STAT_LABLE_WIDTH,											              //WIDTH
			                   LINE_HEIGHT),												                //height
			        		   _toon.GetSkill(cnt).AdjustedBaseValue.ToString());
			}
	}
	
	private void DispplayPointLeft() {
		GUI.Label(new Rect(255, 10, 100, 25), "Points: " + pointLeft.ToString());
	}
	private void DisplayButton() {
		if(GUI.Button(new Rect(Screen.width/ 2 - 50,															//x
			                   STAT_STARTING_POINT + (10 * LINE_HEIGHT),				    //y
			                   STAT_LABLE_WIDTH,											              //WIDTH
			                   LINE_HEIGHT),											                	//height
							   "Create")); {
			GameObject gs = GameObject.Find("__gamesettings");                      //info saveing
			GameSettings gsScript = gs.GetComponent<GameSettings>();
			gsScript.SaveCharacterData();
      Application.LoadLevel("Targeting");                                     //loadinglvl on click ????????
		}
	}
}
		



	